package jp.co.systemi.asp.workflow.peer;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Logger;

import jp.co.systemi.asp.workflow.data.Group;
import jp.co.systemi.asp.workflow.data.GroupMeta;
import jp.co.systemi.asp.workflow.data.User;
import jp.co.systemi.asp.workflow.data.UserGroup;
import jp.co.systemi.asp.workflow.data.UserGroupMeta;
import jp.co.systemi.asp.workflow.data.UserMeta;
import jp.co.systemi.asp.workflow.user.UserSearch.UserSearchModel;

import org.datanucleus.util.StringUtils;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.FilterCriterion;
import org.slim3.datastore.ModelQuery;

public class UserPeer {
    @SuppressWarnings("unused")
    private static final Logger log = Logger
            .getLogger(UserPeer.class.getName());
    private static ResourceBundle rb = ResourceBundle.getBundle(UserPeer.class
            .getName());
    /**
     * 動的に検索条件を設定する
     * @param model
     * @return
     */
    public static ModelQuery<User> getQuery(UserSearchModel model) {
        UserMeta um = UserMeta.get();
        List<FilterCriterion> fcList = new ArrayList<FilterCriterion>();
        if (model != null && !StringUtils.isEmpty(model.getLoginId())) {
            fcList.add(um.loginId.equal(model.getLoginId()));
        }
        ModelQuery<User> query = Datastore.query(um);
        if (fcList.size() > 0) {
            FilterCriterion[] fcArray = new FilterCriterion[fcList.size()];
            fcArray = fcList.toArray(fcArray);
            query = query.filter(fcArray);
        }
        return query;
    }

    public static List<User> doSearch(UserSearchModel model) {
        UserMeta um = UserMeta.get();
        List<User> users = getQuery(model).sort(um.seq.asc).asList();
        UserGroupMeta ugm = UserGroupMeta.get();
        GroupMeta gm = GroupMeta.get();
        for (User user : users) {
            UserGroup main = Datastore.query(ugm).filter(
                    ugm.loginId.equal(user.getLoginId()), ugm.main.equal(true))
                    .asSingle();
            if (main != null) {
                Group group = Datastore.query(gm).filter(
                        gm.code.equal(main.getGroupCode())).asSingle();
                if (group != null) {
                    user.setGroupCode(group.getCode());
                    user.setGroupName(group.getName());
                }
            }
        }
        return users;
    }

    public static String getName(User user) {
        return (user.getLastName() == null ? "" : user.getLastName()) + rb.getString("name.seperator")
                + (user.getFirstName() == null ? "" : user.getFirstName());
    }

    public static User getAdmin() {
        UserMeta um = UserMeta.get();
        User user = Datastore.query(um).filter(um.loginId.startsWith("admin"))
                .asSingle();
        return user;
    }

    public static User getTheDirectorInChargeOfGeneralAffairs() {
        UserGroupMeta ugm = UserGroupMeta.get();
        UserGroup userGroup = Datastore.query(ugm).filter(
                ugm.role.equal(rb
                        .getString("director.general.affairs.department")))
                .asSingle();
        if (userGroup == null) {
            return null;
        }
        UserMeta um = UserMeta.get();
        User user = Datastore.query(um).filter(
                um.loginId.equal(userGroup.getLoginId())).asSingle();
        return user;
    }

    public static List<User> getPage(UserSearchModel model, final int start,
            final int pageSize) {
        final int end = start + pageSize;
        List<User> users = getQuery(model).limit(end).offset(start).asList();
        UserGroupMeta ugm = UserGroupMeta.get();
        GroupMeta gm = GroupMeta.get();
        int i = 0;
        for (User user : users) {
            UserGroup main = Datastore.query(ugm).filter(
                    ugm.loginId.equal(user.getLoginId()), ugm.main.equal(true))
                    .asSingle();
            if (main != null) {
                Group group = Datastore.query(gm).filter(
                        gm.code.equal(main.getGroupCode())).asSingle();
                if (group != null) {
                    user.setGroupCode(group.getCode());
                    user.setGroupName(group.getName());
                }
            }
            user.setSeq(start + i++);
        }
        return users;
    }

    public static int getSize(UserSearchModel model) {
        return getQuery(model).count();
    }

}
